package javanio20;
import java.nio.IntBuffer;
public class IntBufferDemo03
{


	public static void main(String[] args)
	{
		IntBuffer intBuf = IntBuffer.allocate(10);
		IntBuffer subBuf = null;
		System.out.println("写入数据前：position = "+intBuf.position()+
				"，limit = "+intBuf.limit()+"，capacity="+intBuf.capacity());
		for(int i = 0;i<10;i++)
		{
			intBuf.put(i*2+1);
		}
		intBuf.position(2);
		intBuf.limit(6);
		//只读
		subBuf = intBuf.slice().asReadOnlyBuffer();
		for(int i = 0;i<subBuf.capacity();i++)
		{
			int temp = subBuf.get(i);
			subBuf.put(temp-1);
		}
		System.out.println("写入数据后：position = "+intBuf.position()+
				"，limit = "+intBuf.limit()+"，capacity="+intBuf.capacity());
		//重设缓冲区，让limit = position; position=0;
		intBuf.flip();
		intBuf.limit(intBuf.capacity());
		System.out.println("执行flip后：position = "+intBuf.position()+
				"，limit = "+intBuf.limit()+"，capacity="+intBuf.capacity());
		while(intBuf.hasRemaining())
		{
			System.out.print(intBuf.get());
			if(intBuf.position() < intBuf.limit())
			{
				System.out.print("、");
			}
		}
		
		System.out.println("\n执行输出后：position = "+intBuf.position()+
				"，limit = "+intBuf.limit()+"，capacity="+intBuf.capacity());
	}

}
